package com.yukexx.common.utils;

import java.io.*;
import java.net.SocketException;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.net.ftp.*;

public class Ftpc {
	private String ip;  
	private  int port = 21;
	private String uname;
	private String passwd;

	private FTPClient ftp = null;

	public Ftpc(String ip, int port, String username, String password) {
		this.ip = ip;
		this.port = port;
		this.uname = username;
		this.passwd = password;
		ftp = new FTPClient();
	}

	public Ftpc(String ip, String username, String password) {
		this.ip = ip;
		this.uname = username;
		this.passwd = password;
		ftp = new FTPClient();
	}

	boolean login(String username, String password) throws SocketException,
			IOException {
		ftp.connect(ip, port);
		int reply = ftp.getReplyCode();
		if (!FTPReply.isPositiveCompletion(reply)) {
			ftp.disconnect();
			return false;
		}
		boolean suc = ftp.login(uname, passwd);
		return suc;
	}

	boolean mkdir(String remotePath) throws IOException {
		return ftp.makeDirectory(remotePath);
	}

	boolean store(String remotePath, String file, InputStream input)
			throws IOException {
		boolean suc = ftp.changeWorkingDirectory(remotePath);
		if (suc) {
			ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
			suc = ftp.storeFile(file, input);
			return suc;
		}
		return false;
	}

	String localPath(String local) {
		return (local.endsWith("/") ? local : local + "/");
	}

	boolean get(String file, String local) throws IOException {
		boolean suc = false;
		String s = localPath(local);
		FTPFile[] fs = ftp.listFiles();
		for (FTPFile ff : fs) {
			if (ff.getName().equals(file)) {
				File localFile = new File(s + ff.getName());
				OutputStream is = new FileOutputStream(localFile);
				suc = ftp.retrieveFile(ff.getName(), is);
				is.close();
				return suc;
			}
		}
		return suc;
	}
    
	public boolean upload(String remotePath, String file, InputStream input) throws SocketException, IOException {
		ftp.setControlEncoding("UTF-8");
		boolean suc = login(uname, passwd);
		if (suc) {
			mkdir(remotePath);
			suc = store(remotePath, file, input);
		}
		input.close();
		ftp.logout();
		return suc;
	}

	public boolean download(String remotePath, String file, String local)
			throws SocketException, IOException {
		boolean suc = login(uname, passwd);
		if (suc) {
			suc = ftp.changeWorkingDirectory(remotePath);
			if (suc) {
				suc = get(file, local);
			}
		}
		ftp.logout();
		return suc;
	}
	/**
	 * 文件下载方法
	 * @param response
	 * @param fileName 文件名
	 * @param path 文件所在的工作路径
	 * @return 如果连接失败返回false
	 * @throws SocketException
	 * @throws IOException
	 */
    public boolean downloadFile(HttpServletResponse response, String fileName, String path) throws SocketException, IOException{ 
    	ftp.setControlEncoding("UTF-8");
    	boolean suc = login(uname, passwd);
    	if(suc){
        response.setCharacterEncoding("UTF-8");  
        response.setContentType("multipart/form-data;charset=UTF-8");  
        try {  
            ftp.changeWorkingDirectory(path);  
            FTPFile[] fs = ftp.listFiles();  
            for(FTPFile ff: fs){  
                if(ff.getName().equals(fileName)){  
                    response.setHeader("Content-Disposition", "attachment;fileName="+ new String( ff.getName().getBytes("gb2312"), "ISO8859-1" ) );  
                    OutputStream os = response.getOutputStream();  
                    ftp.retrieveFile(ff.getName(), os);  
                    os.flush();  
                    os.close();  
                    break;  
                }  
            }  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        }
    	ftp.logout();
        return suc;  
    } 
	public boolean deleteFile(String remotePath, String filename)
			throws SocketException, IOException {
		boolean suc = login(uname, passwd);
		if (suc) {
			suc = ftp.changeWorkingDirectory(remotePath);
			ftp.dele(filename);
		}
		ftp.logout();
		return suc;
	}
}
